/**
 * Copyright (c) 2011 Roundarch (www.roundarch.com)
 *
 * Author Joshua Jung
 *
 * ---
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.roundarch.flex.controls.dateSliderClasses
{

    /**
     * Utility class for useful functions for the DateSlider.
     *
     * @author jjung
     */
    public class DateSliderUtil
    {
        /**
         *  Returns the number of days in a month. Ripped from CalendarLayout.
         *
         * I can't believe such a useful function was not put into a utility class.
         * Le duh. @#$% Adobe.
         */
        public static function getNumberOfDaysInMonth(year : int, month : int) : int
        {
            var n : int;

            if (month == 1) // Feb
            {
                if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) // leap year
                {
                    n=29;
                }
                else
                {
                    n=28;
                }
            }
            else if (month == 3 || month == 5 || month == 8 || month == 10)
            {
                n=30;
            }
            else
            {
                n=31;
            }

            return n;
        }

        //---------------------------------------------------------------------
        //
        //  Static Methods
        //
        //---------------------------------------------------------------------
        /**
         * Given a time value, this returns the exact time value of the
         * beginning of that month. In milliseconds.
         */
        public static function timeToMonthBegin(time : Number) : Number
        {
            var date : Date=new Date(time);
            var month : int=date.month;
            var year : int=date.fullYear;

            var dateMonthBegin : Date=new Date(year, month);

            return dateMonthBegin.getTime();
        }

        /**
         * Given a time value, this returns the exact time value of the
         * beginning of the next month. In milliseconds.
         */
        public static function timeToMonthEnd(time : Number) : Number
        {
            var date : Date=new Date(time);
            var month : int=date.month + 1;
            var year : int=date.fullYear;

            if (month > 11)
            {
                month-=12;
                year++;
            }

            var dateMonthEnd : Date=new Date(year, month);

            return dateMonthEnd.getTime();
        }

        /**
         * Given a time value, this returns the length - in milliseconds -
         * of the month in which that time value resides.
         */
        public static function timeToMonthLength(time : Number) : Number
        {
            var date : Date=new Date(time);

            return DateSliderConst.MILLI_IN_DAY * getNumberOfDaysInMonth(date.fullYear, date.month);
        }

        /**
         * Given a time value, this returns the exact time value of the
         * beginning of that year. In milliseconds.
         */
        public static function timeToYearBegin(time : Number) : Number
        {
            var date : Date=new Date(time);
            var year : int=date.fullYear;

            var dateYearBegin : Date=new Date(year, 0);

            return dateYearBegin.getTime();
        }

        /**
         * Given a time value, this returns the exact time value of the
         * beginning of the next year. In milliseconds.
         */
        public static function timeToYearEnd(time : Number) : Number
        {
            var date : Date=new Date(time);
            var year : int=date.fullYear;

            var dateYearEnd : Date=new Date(year + 1, 0);

            return dateYearEnd.getTime();
        }

        /**
         * Given a time value, this returns the length - in milliseconds -
         * of the year in which that time value resides.
         */
        public static function timeToYearLength(time : Number) : Number
        {
            var date : Date=new Date(time);

            if (((date.fullYear % 4 == 0) && (date.fullYear % 100 != 0)) || (date.fullYear % 400 == 0)) // leap year
            {
                return DateSliderConst.MILLI_IN_DAY * 366;
            }

            return DateSliderConst.MILLI_IN_DAY * 365;
        }
    }
}
